'use strict';
const AV = require('leanengine');


/**
 * 首页数据
 */
exports.index = function (req, res, next) {

    (async function () {

        try {
            //广告数据
            let ads = await new AV.Query("Ad")
                .ascending('order')
                .find();

            //频道数据
            let channel = await new AV.Query("Channel")
                .ascending('order')
                .find();

            //品牌分类
            let brands = await new AV.Query("Brand")
                .ascending('indexRecommendSort')
                .equalTo('isIndexRecommend', true)
                .limit(3)
                .find();

            //新品首发
            let newGoods = await new AV.Query("Goods")
                .ascending('order')
                .equalTo('isNew', 1)
                .limit(4)
                .find();

            //人气推荐
            let bestGoods = await new AV.Query("Goods")
                .ascending('order')
                .equalTo('isBest', 1)
                .limit(3)
                .find();

            //专题精选
            let topic = await new AV.Query("Topic")
                .ascending('order')
                .limit(3)
                .find();

            return res.json({
                ads: ads,
                channel: channel,
                brands: brands,
                newGoods: newGoods,
                bestGoods: bestGoods,
                topic: topic
            });
        } catch (e){
            next(e);
        }
    })();
};

